﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Bass.Tools;
using MySql.Data.MySqlClient;

public partial class admin_add_item : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["adminname"]) == "")
        {
            Response.Write("<script language=javascript>alert('您没有登录');window.parent.document.location.href ='admin_login.aspx'</script>");
        }
        else if (Convert.ToString(Session["adminkind"]) != "product")
        {
            Response.Write("<script language=javascript>alert('您不是产品管理员，无权访问此页！');location='admin_login.aspx'</script>");
        }
        if (!IsPostBack)
        {
            //OleDbConnection Conn = new OleDbConnection();
            //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
            //Conn.Open();

            //取到连接字符串
            string propertiesPath = Server.MapPath("../mysqldb.properties");
            mytools mt = new mytools();
            string ConnString = mt.getMysqlConn(propertiesPath);

            //打开连接
            MySqlConnection myConnection = new MySqlConnection(ConnString);
            myConnection.Open();
            
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter("select * from productkind where kindstatus='A'", myConnection);
            da.Fill(ds, "productkind");
            DataRow[] rows = ds.Tables["productkind"].Select();
            foreach (DataRow row in rows)
            {
                ListItem newitem = new ListItem();
                newitem.Text = row["productkind"].ToString();
                newitem.Value = row["productkinden"].ToString();
                productkind.Items.Add(newitem);
            }

            DataSet ds1 = new DataSet();
            MySqlDataAdapter da1 = new MySqlDataAdapter("select * from productsource where sourcestatus='A' ", myConnection);
            da1.Fill(ds1, "productsource");
            DataRow[] rows1 = ds1.Tables["productsource"].Select();
            foreach (DataRow row1 in rows1)
            {
                ListItem newitem1 = new ListItem();
                newitem1.Text = row1["productsource"].ToString();
                newitem1.Value = row1["productsourceen"].ToString();
                productsource.Items.Add(newitem1);
            }
            myConnection.Close();
        }
    }

    protected void ClearTextBox()
    {
        producttitle.Text = string.Empty;
        productprice.Text = string.Empty;
        introduction.Text = string.Empty;
        producturl.Text = string.Empty;
        detail.Text = string.Empty;
        detail2.Text = string.Empty;
        detail3.Text = string.Empty;
        detail4.Text = string.Empty;
        detail5.Text = string.Empty;
        //productkind.Text = string.Empty;
        //source.Text = string.Empty;
        abroadornot.Text = string.Empty;
        recommendornot.Text = string.Empty;
        picaddr.Text = string.Empty;
        detailurl.Text = string.Empty;
        detailurl2.Text = string.Empty;
        detailurl3.Text = string.Empty;
        detailurl4.Text = string.Empty;
        detailurl5.Text = string.Empty;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ClearTextBox();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //string filePath = "", fileExtName = "", mFileName, mPath, mTotalPath;
        //取到连接字符串
        string propertiesPath = Server.MapPath("../mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        string producttitle1 = producttitle.Text.ToString();
        string productprice1 = productprice.Text.ToString();
        string introduction1 = introduction.Text.ToString();
        string introduction2e = introduction2.Text.ToString();
        string introduction3e = introduction3.Text.ToString();
        string introduction4e = introduction4.Text.ToString();
        string introduction5e = introduction5.Text.ToString();
        string introduction6e = introduction6.Text.ToString();
        string introduction7e = introduction7.Text.ToString();
        string introduction8e = introduction8.Text.ToString();
        string introduction9e = introduction9.Text.ToString();
        string introduction10e = introduction10.Text.ToString();

        string producturl1 = producturl.Text.ToString();
        string detail1 = detail.Text.ToString();
        string productkinden = productkind.SelectedItem.Value;
        string productkindcn = productkind.SelectedItem.Text;
        string productsourceen = productsource.SelectedItem.Value;
        string productsourcecn = productsource.SelectedItem.Text;
        string abroadornot1 = abroadornot.Text.ToString();
        string recommendornot1 = recommendornot.Text.ToString();
        DateTime dt = Convert.ToDateTime(DateTime.Now);
        string addtime1 = dt.ToString("yyyyMMddHHmmss");
        string productnum1 = productkinden + productsourceen + addtime1;
        string addadminname1 = Convert.ToString(Session["adminname"]);
        string detailurle = detailurl.Text.ToString();
        string detail2e = detail2.Text.ToString();
        string detailurl2e = detailurl2.Text.ToString();
        string detail3e = detail3.Text.ToString();
        string detailurl3e = detailurl3.Text.ToString();
        string detail4e = detail4.Text.ToString();
        string detailurl4e = detailurl4.Text.ToString();
        string detail5e = detail5.Text.ToString();
        string detailurl5e = detailurl5.Text.ToString();
        string picaddr1 = picaddr.Text.ToString();

        try
        {
            string sqlinsert = "insert into product_info (producttitle,productprice,productnum,picaddr,introduction,introduction2,introduction3,introduction4,introduction5,introduction6,introduction7,introduction8,introduction9,introduction10,producturl,detail,productkind,abroadornot,recommendornot,addtime,source,addadminname,detailurl,detail2,detailurl2,detail3,detailurl3,detail4,detailurl4,detail5,detailurl5,beclickedtotalnumber,productstatus)values" +
                "('" + producttitle1 + "','" + productprice1 + "','" + productnum1 + "','" + picaddr1 + "','" + introduction1 + "','" + introduction2e + "','" + introduction3e + "','" + introduction4e + "','" + introduction5e + "','" + introduction6e + "','" + introduction7e + "','" + introduction8e + "','" + introduction9e + "','" + introduction10e + "','" + producturl1 + "','" + detail1 + "','" + productkindcn + "','" + abroadornot1 + "','" + recommendornot1 + "','" + addtime1 + "','" + productsourcecn + "','" + addadminname1 + "','" + detailurle + "','" + detail2e + "','" + detailurl2e + "','" + detail3e + "','" + detailurl3e + "','" + detail4e + "','" + detailurl4e + "','" + detail5e + "','" + detailurl5e + "',0,'A')";
            MySqlCommand scd = new MySqlCommand(sqlinsert, myConnection);
            scd.ExecuteNonQuery();
            Response.Write("<script lanuage=javascript>alert('添加产品成功！');location ='add_item.aspx'</script>");
            myConnection.Close();
        }
        catch (Exception err)
        {
            Response.Write(err.ToString());
            Response.Write("<script lanuage=javascript>alert('添加产品失败！');");
            //Response.Write("<script lanuage=javascript>alert('添加产品失败！');location ='add_item.aspx'</script>");
            myConnection.Close();
        }

    }    

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string abroadornot1 = this.abroadornot.Text.ToString();
        if (abroadornot1 == "y" || abroadornot1 == "n")
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string recommendornot1 = this.recommendornot.Text.ToString();
        if (recommendornot1 == "y" || recommendornot1 == "n")
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}
